/**
 * Copyright(c) by JNet Smartware Group.
 * All rights reserved. Not distribution or redistribution without
 * any permission of JNet Smartware Group.
 * 
 * @author thuc
 * 2010/08/04
 * Program: SWTRegisterDrinkEntry.java
 */
package vn.smartware.co.client.register;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.custom.TableTree;
import org.eclipse.swt.custom.TableTreeItem;
import org.eclipse.swt.events.FocusAdapter;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;
import org.eclipse.wb.swt.SWTResourceManager;

import vn.smartware.co.commons.Const;
import vn.smartware.co.commons.SWTUtils;
import vn.smartware.co.dto.MstDrinkDTO;
import vn.smartware.co.dto.MstPriceDTO;
import vn.smartware.co.dto.MstUnitDTO;
import vn.smartware.co.forms.RegisterDrinkForm;
import vn.smartware.co.forms.RegisterFoodForm;
import vn.smartware.co.framework.common.BaseConstant;
import vn.smartware.co.framework.exception.ClientException;
import vn.smartware.co.framework.exception.FatalException;
import vn.smartware.co.framework.exception.ServerException;
import vn.smartware.co.framework.exception.SessionTimeOutException;
import vn.smartware.co.framework.plugin.LangResources;
import vn.smartware.co.framework.swt.base.JNetComposite;
import vn.smartware.co.framework.utils.MsgBoxUtils;
import vn.smartware.co.java.net.commons.SortUtil;
import vn.smartware.co.java.net.commons.Utils;

/**
 * SWTRegisterDrinkEntry
 * @author thuctt
 */
public class SWTRegisterDrinkEntry extends JNetComposite {

	private static Log log = LogFactory.getLog(SWTRegisterDrinkEntry.class);
	
	private TableTree tableTree;

	private Button btnRemove;

	private Button btnAdd;

	private Combo cmbMoney;

	private Text txtUnitPrice;

	private Table tblUnitPrice;
	
	private Combo cmbType;

	private Combo cmbUnit;

	private Group grpSearch;

	private CLabel clblDrinkNo;

	private CLabel clblMessage;

	private Button btnSave;

	private Button btnClear;

	private Text txtDescription;

	private Text txtDrinkName;

	private Table tblInfo;

	private Button btnSearch;

	private Text txtNoEnd;

	private Text txtNoStart;

	private Button btnEdit;

	private Button btnClose;

	private Button btnNew;

	private Button btnDelete;

	private Button btnApply;
	
	private Group grpDetail;
	
	private static final String ID = "ID";
	
	private static final int TBL_INFO_ROW_COUNT = 12;
	
	private static final int TBL_PRICE_ROW_COUNT = 3;
	
	private String mode;
	
	private int edittingRow = -1;
	
	private int priceEditRow = -1;
	
	private ArrayList drinkList = null;
	
	private ArrayList priceList = null;
	
	private boolean saveFlg = false;
	
	/**
	 * Constructor.
	 * @param parent 
	 * @param style 
	 * @param prgName 
	 */
	public SWTRegisterDrinkEntry(Composite parent, int style, String prgName) {

		super(parent, style, prgName);

		try {
//			setBackground(BaseConstant.COLOR_BGR_BASE);
//			
//			executeInit();
			
			if (!initData()){
				lockScreen(true);
				btnClose.setEnabled(true);
				btnClose.setFocus();
				return;
			}
			
			clearAction();

		} catch (Exception e) {
			log.error("Exception.", e);
			fatalErrorMsgBox();
		}
	}
	
	/**
	 * init controls
	 */
	protected void initialize() {

		grpSearch = new Group(this, SWT.NONE);
		grpSearch.setBounds(13, 5, 784, 59);
		grpSearch.setBackground(BaseConstant.COLOR_BGR_BASE);

		final CLabel lblSearchNo = new CLabel(grpSearch, SWT.CENTER | SWT.BORDER);
		lblSearchNo.setBounds(21, 20,100, 22);
		lblSearchNo.setFont(BaseConstant.FONT_BASE_LABEL_NAME);
		lblSearchNo.setText(LangResources.getText("SWTRegisterDrinkEntry.lblSearchNo"));

		txtNoStart = new Text(grpSearch, SWT.BORDER | SWT.RIGHT);
		txtNoStart.setBounds(127, 20,120, 22);
		txtNoStart.setTextLimit(3);
		txtNoStart.setTabs(1);
		txtNoStart.setFont(BaseConstant.FONT_BASE_TEXT);
		txtNoStart.setBackground(SWTResourceManager.getColor(255, 255, 255));

		final Label lbl1 = new Label(grpSearch, SWT.CENTER);
		lbl1.setBounds(287, 20,22, 18);
		lbl1.setText("-");
		lbl1.setFont(BaseConstant.FONT_BASE_LABEL);
		lbl1.setBackground(BaseConstant.COLOR_BGR_BASE);

		txtNoEnd = new Text(grpSearch, SWT.BORDER | SWT.RIGHT);
		txtNoEnd.setBounds(342, 20,120, 22);
		txtNoEnd.setTextLimit(3);
		txtNoEnd.setTabs(2);
		txtNoEnd.setFont(BaseConstant.FONT_BASE_TEXT);
		txtNoEnd.setBackground(SWTResourceManager.getColor(255, 255, 255));
		
		btnSearch = new Button(grpSearch, SWT.NONE);
		btnSearch.setBounds(655, 20,100, 23);
		btnSearch.setText(LangResources.getText("btnSearch"));
		btnSearch.setFont(BaseConstant.FONT_BASE_BUTTON);
		btnSearch.setCursor(SWTResourceManager.getCursor(SWT.CURSOR_HAND));
		btnSearch.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				searchAction();
			}
		});
		
		this.getShell().setDefaultButton(btnSearch);
		
		final Label line1 = new Label(this, SWT.SEPARATOR | SWT.HORIZONTAL);
		line1.setBounds(0, 75, 812, 15);
		line1.setBackground(BaseConstant.COLOR_BGR_BASE);

		tableTree = new TableTree(this, SWT.FULL_SELECTION | SWT.BORDER);
		tableTree.setFont(BaseConstant.FONT_BASE_LABEL);
		tableTree.setBounds(35, 105, 735, 230);
		
		tblInfo = tableTree.getTable();
		tblInfo.addSelectionListener(new SelectionAdapter() {
			public void widgetDefaultSelected(final SelectionEvent arg0) {
				TableTreeItem item = tableTree.getSelection()[0];
				Object data = item.getData(Const.ITEMID);
				if (null == data){
					newAction();					
				}else{
					editAction();
				}
			}
		});
		tblInfo.setLinesVisible(true);
		tblInfo.setHeaderVisible(true);
		tblInfo.setData(BaseConstant.TABLE_ROW_COUNT, Integer.toString(TBL_INFO_ROW_COUNT));
		
		final TableColumn tcolDrink = new TableColumn(tblInfo, SWT.NONE);
		tcolDrink.setWidth(220);
		tcolDrink.setText(LangResources.getText("SWTRegisterDrinkEntry.tblInfo.tcolDrink"));
		
		final TableColumn tcolType = new TableColumn(tblInfo, SWT.NONE);
		tcolType.setWidth(100);
		tcolType.setText(LangResources.getText("SWTRegisterDrinkEntry.tblInfo.tcolType"));
		
		final TableColumn tcolPrice = new TableColumn(tblInfo, SWT.RIGHT);
		tcolPrice.setAlignment(SWT.RIGHT);
		tcolPrice.setWidth(90);
		tcolPrice.setText(LangResources.getText("SWTRegisterDrinkEntry.tblInfo.tcolPrice"));
		
		final TableColumn tcolUnit = new TableColumn(tblInfo, SWT.NONE);
		tcolUnit.setWidth(85);
		
		final TableColumn tcolDescription = new TableColumn(tblInfo, SWT.NONE);
		tcolDescription.setWidth(218);
		tcolDescription.setText(LangResources.getText("SWTRegisterDrinkEntry.tblInfo.tcolDescription"));
		
		btnDelete = new Button(this, SWT.NONE);
		btnDelete.setBounds(458, 341,100, 23);
		btnDelete.setFont(BaseConstant.FONT_BASE_BUTTON);
		btnDelete.setText(LangResources.getText("btnDelete"));
		btnDelete.setCursor(SWTResourceManager.getCursor(SWT.CURSOR_HAND));
		btnDelete.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				deleteAction();
			}
		});

		btnNew = new Button(this, SWT.NONE);
		btnNew.setBounds(564, 341,100, 23);
		btnNew.setFont(BaseConstant.FONT_BASE_BUTTON);
		btnNew.setText(LangResources.getText("btnNew"));
		btnNew.setCursor(SWTResourceManager.getCursor(SWT.CURSOR_HAND));
		btnNew.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				newAction();
			}
		});

		btnEdit = new Button(this, SWT.NONE);
		btnEdit.setBounds(670, 341,100, 23);
		btnEdit.setFont(BaseConstant.FONT_BASE_BUTTON);
		btnEdit.setText(LangResources.getText("btnEdit"));
		btnEdit.setCursor(SWTResourceManager.getCursor(SWT.CURSOR_HAND));
		btnEdit.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				editAction();
			}
		});
		
		final Label line2 = new Label(this, SWT.HORIZONTAL | SWT.SEPARATOR);
		line2.setBounds(0, 380, 812, 15);
		line2.setBackground(BaseConstant.COLOR_BGR_BASE);
		
		grpDetail = new Group(this, SWT.NONE);
		grpDetail.setSize(782, 211);
		grpDetail.setLocation(15, 401);
		grpDetail.setBackground(BaseConstant.COLOR_BGR_BASE);

		final CLabel lblNo = new CLabel(grpDetail, SWT.BORDER | SWT.CENTER);
		lblNo.setBounds(10, 25, 100, 22);
		lblNo.setFont(BaseConstant.FONT_BASE_LABEL_NAME);
		lblNo.setText(LangResources.getText("SWTRegisterDrinkEntry.lblNo"));

		clblDrinkNo = new CLabel(grpDetail, SWT.RIGHT | SWT.SHADOW_IN | SWT.BORDER);
		clblDrinkNo.setBounds(116, 25, 60, 22);
		clblDrinkNo.setFont(BaseConstant.FONT_BASE_LABEL);
		clblDrinkNo.setBackground(BaseConstant.COLOR_BGR_LABEL);

		txtDrinkName = new Text(grpDetail, SWT.BORDER);
		txtDrinkName.setBounds(182, 25, 200, 22);
		txtDrinkName.setTextLimit(50);
		txtDrinkName.setFont(BaseConstant.FONT_BASE_TEXT);
		txtDrinkName.setBackground(BaseConstant.COLOR_TEXT_REQUIRED);

		final CLabel lblType = new CLabel(grpDetail, SWT.CENTER | SWT.BORDER);
		lblType.setBounds(10, 53, 100, 22);
		lblType.setFont(BaseConstant.FONT_BASE_LABEL);
		lblType.setText(LangResources.getText("SWTRegisterDrinkEntry.lblType"));

		cmbType = new Combo(grpDetail, SWT.READ_ONLY);
		cmbType.setBounds(116, 53, 142, 24);
		cmbType.setTextLimit(200);
		cmbType.setFont(SWTResourceManager.getFont("Microsoft Sans Serif", 10, SWT.NONE));
		cmbType.setBackground(SWTResourceManager.getColor(250, 254, 200));

		final CLabel lblUnit = new CLabel(grpDetail, SWT.BORDER | SWT.CENTER);
		lblUnit.setBounds(10, 81, 100, 22);
		lblUnit.setFont(BaseConstant.FONT_BASE_LABEL);
		lblUnit.setText(LangResources.getText("SWTRegisterDrinkEntry.lblUnit"));

		cmbUnit = new Combo(grpDetail, SWT.READ_ONLY);
		cmbUnit.setBounds(116, 81, 142, 24);
		cmbUnit.setTextLimit(200);
		cmbUnit.setFont(BaseConstant.FONT_BASE_LABEL);
		cmbUnit.setBackground(BaseConstant.COLOR_TEXT_REQUIRED);

		final CLabel lblDescription = new CLabel(grpDetail, SWT.BORDER | SWT.CENTER);
		lblDescription.setBounds(10, 109, 100, 22);
		lblDescription.setFont(BaseConstant.FONT_BASE_LABEL_NAME);
		lblDescription.setText(LangResources.getText("SWTRegisterDrinkEntry.lblDescription"));

		txtDescription = new Text(grpDetail, SWT.BORDER);
		txtDescription.setBounds(116, 109, 266, 50);
		txtDescription.setTextLimit(1024);
		txtDescription.setFont(BaseConstant.FONT_BASE_TEXT);
		txtDescription.setBackground(BaseConstant.COLOR_TEXT_NORMAL);

		final Group grpPrice = new Group(grpDetail, SWT.NONE);
		grpPrice.setBounds(400, 15, 346, 146);
		grpPrice.setText(LangResources.getText("SWTRegisterDrinkEntry.grpPrice"));
		grpPrice.setBackground(BaseConstant.COLOR_BGR_BASE);

		tblUnitPrice = new Table(grpPrice, SWT.FULL_SELECTION | SWT.BORDER);
		tblUnitPrice.setFont(BaseConstant.FONT_BASE_LABEL);
		tblUnitPrice.setBounds(20, 22, 241, 84);
		tblUnitPrice.setLinesVisible(true);
		tblUnitPrice.setHeaderVisible(true);
		tblUnitPrice.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(final SelectionEvent arg0) {
				clblMessage.setText("");
				priceEditRow = tblUnitPrice.getSelectionIndex();
				TableItem item = tblUnitPrice.getItem(priceEditRow);
				MstPriceDTO data = (MstPriceDTO)item.getData();
				if (null == data){//New
					cmbMoney.select(0);
					txtUnitPrice.setText("");
					txtUnitPrice.setFocus();

				}else{//Edit
					SWTUtils.ComboProcess.setDisplayLabel(cmbMoney, data.getMoneyType());
					txtUnitPrice.setText(Utils.formatDouble(Utils.toEmpty(data.getPrice())));
					txtUnitPrice.setFocus();
				}
			}
		});
		tblUnitPrice.setData(BaseConstant.TABLE_ROW_COUNT, Integer.toString(TBL_PRICE_ROW_COUNT));

		final TableColumn tcol0 = new TableColumn(tblUnitPrice, SWT.RIGHT);
		tcol0.setWidth(0);
		
		final TableColumn tcolUnitPrice = new TableColumn(tblUnitPrice, SWT.RIGHT);
		tcolUnitPrice.setWidth(130);
		tcolUnitPrice.setText(LangResources.getText("SWTRegisterDrinkEntry.tblUnitPrice.tcolUnitPrice"));

		final TableColumn tcolCurrency = new TableColumn(tblUnitPrice, SWT.RIGHT);
		tcolCurrency.setWidth(90);
		tcolCurrency.setText(LangResources.getText("SWTRegisterDrinkEntry.tblUnitPrice.tcolCurrency"));

		txtUnitPrice = new Text(grpPrice, SWT.BORDER | SWT.RIGHT);
		txtUnitPrice.setTextLimit(11);
		txtUnitPrice.setFont(BaseConstant.FONT_BASE_TEXT);
		txtUnitPrice.setBackground(BaseConstant.COLOR_TEXT_REQUIRED);
		txtUnitPrice.setBounds(20, 113, 134, 22);
		txtUnitPrice.addFocusListener(new FocusAdapter() {
			public void focusLost(final FocusEvent arg0) {
				if (!Utils.isNumeric(txtUnitPrice.getText())){
					log.warn("Input string is not a number.");
					return;
				}
				txtUnitPrice.setText(Utils.formatDouble(txtUnitPrice.getText()));
			}
			public void focusGained(final FocusEvent arg0) {
				txtUnitPrice.setText(Utils.toNormalNumber(txtUnitPrice.getText()));
			}
		});

		cmbMoney = new Combo(grpPrice, SWT.READ_ONLY);
		cmbMoney.setTextLimit(200);
		cmbMoney.setFont(BaseConstant.FONT_BASE_LABEL);
		cmbMoney.setBounds(154, 112, 106, 24);

		btnAdd = new Button(grpPrice, SWT.NONE);
		btnAdd.setFont(BaseConstant.FONT_BASE_BUTTON);
		btnAdd.setBounds(267, 113, 70, 23);
		btnAdd.setCursor(SWTResourceManager.getCursor(SWT.CURSOR_HAND));
		btnAdd.setText(LangResources.getText("btnAdd"));
		btnAdd.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				addPriceAction();
			}
		});

		btnRemove = new Button(grpPrice, SWT.NONE);
		btnRemove.setFont(BaseConstant.FONT_BASE_BUTTON);
		btnRemove.setBounds(267, 83, 70, 23);
		btnRemove.setText(LangResources.getText("btnRemove"));
		btnRemove.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				removePriceAction();
			}
		});
		
		btnApply = new Button(grpDetail, SWT.NONE);
		btnApply.setBounds(646, 178, 100, 23);
		btnApply.setFont(BaseConstant.FONT_BASE_BUTTON);
		btnApply.setText(LangResources.getText("btnApply"));
		btnApply.setCursor(SWTResourceManager.getCursor(SWT.CURSOR_HAND));
		btnApply.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				applyAction();
			}
		});

		final Label line3 = new Label(this, SWT.HORIZONTAL | SWT.SEPARATOR);
		line3.setBounds(0, 618, 812, 15);
		line3.setBackground(SWTResourceManager.getColor(234, 234, 234));

		btnSave = new Button(this, SWT.NONE);
		btnSave.setBounds(485, 637, 100, 23);
		btnSave.setFont(BaseConstant.FONT_BASE_BUTTON);
		btnSave.setText(LangResources.getText("btnSave"));
		btnSave.setCursor(SWTResourceManager.getCursor(SWT.CURSOR_HAND));
		btnSave.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				saveAction();
			}
		});

		btnClear = new Button(this, SWT.NONE);
		btnClear.setBounds(591, 637, 100, 23);
		btnClear.setFont(BaseConstant.FONT_BASE_BUTTON);
		btnClear.setText(LangResources.getText("btnClear"));
		btnClear.setCursor(SWTResourceManager.getCursor(SWT.CURSOR_HAND));
		btnClear.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				clearAction();
			}
		});

		btnClose = new Button(this, SWT.NONE);
		btnClose.setBounds(697, 637, 100, 23);
		btnClose.setFont(BaseConstant.FONT_BASE_BUTTON);
		btnClose.setText(LangResources.getText("btnClose"));
		btnClose.setCursor(SWTResourceManager.getCursor(SWT.CURSOR_HAND));
		btnClose.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				if (!closeAction()){
					btnClose.setFocus();
				}
			}
		});

		clblMessage = new CLabel(this, SWT.SHADOW_IN | SWT.CENTER | SWT.BORDER);
		clblMessage.setBounds(15, 635, 250, 25);
		clblMessage.setForeground(BaseConstant.COLOR_LINK_LABEL);
		clblMessage.setFont(BaseConstant.FONT_BOLD_LABEL);
		clblMessage.setBackground(BaseConstant.COLOR_BGR_LABEL);
		clblMessage.setText("");
	}

	/**
	 * clearAction
	 */
	protected void clearAction() {
		
		SWTUtils.OperatorUI.clearAll(this.getChildren());
		
		SWTUtils.OperatorUI.setEnableAll(grpSearch.getChildren(), true);
		Control[] infoGrp = new Control[]{
				tblInfo,
				btnDelete,
				btnNew,
				btnEdit
		};
		SWTUtils.OperatorUI.setEnableAll(infoGrp, false);
		SWTUtils.OperatorUI.setEnableAll(grpDetail.getChildren(), false);
		btnSave.setEnabled(false);
		
		this.mode = null;
		this.edittingRow = -1;
		this.priceEditRow = -1;
		this.drinkList = null;
		this.priceList = null;
		saveFlg = false;
		
		txtNoStart.setFocus();
	}

	/**
	 * deleteAction
	 */
	@SuppressWarnings("unchecked")
	protected void deleteAction() {
		
		try {
			clblMessage.setText("");
			
			TableTreeItem item = tableTree.getSelection()[0];
			MstDrinkDTO drink = (MstDrinkDTO)item.getData(Const.ITEMID);
			if (null == drink){
				MsgBoxUtils.errorMsgBox("errors.NOT_SELECTED_RECORD");
				btnDelete.setFocus();
				return;
			}
			//
			if (MsgBoxUtils.questionMsgBox("question.BE_SURE") == SWT.NO){
				btnDelete.setFocus();
				return;
			}
			//
			int idx = Integer.parseInt((String)item.getData(ID));
			if (!Utils.isNullEmpty(drink.getDrinkNo())){
				drink.setProcessMode(Const.MODE_DEL);
				drinkList.set(idx, drink);
			}else{
				drinkList.remove(idx);
			}
			//
			displayDrinkList(drinkList);
			btnSave.setEnabled(true);
			saveFlg = true;
			tblInfo.setFocus();
			
		}catch (ClientException e) {
			log.error("ClientException", e);
		}
	}

	/**
	 * displayAction
	 */
	protected void searchAction() {
		
		try {
			// check action
			if (!Utils.isNullEmpty(txtNoStart.getText())
					&& !Utils.isNumeric(txtNoStart.getText().trim())){
				MsgBoxUtils.errorMsgBox("errors.NUMERIC_FORMAT", new Object[]{LangResources.getText("SWTRegisterDrinkEntry.lblSearchNo")});
				txtNoStart.setFocus();
				return;
			}
			if (!Utils.isNullEmpty(txtNoEnd.getText())
					&& !Utils.isNumeric(txtNoEnd.getText().trim())){
				MsgBoxUtils.errorMsgBox("errors.NUMERIC_FORMAT", new Object[]{LangResources.getText("SWTRegisterDrinkEntry.lblSearchNo")});
				txtNoEnd.setFocus();
				return;
			}
			if (!Utils.isNullEmpty(txtNoStart.getText()) && !Utils.isNullEmpty(txtNoStart.getText())) {
				int noS = Integer.parseInt(Utils.toNormalNumber(txtNoStart.getText()));
				int noE = Integer.parseInt(Utils.toNormalNumber(txtNoEnd.getText()));
				if (noS > noE){
					MsgBoxUtils.errorMsgBox("errors.NUMBER_GREATER_COMPARE", 
							new Object[]{LangResources.getText("SWTRegisterDrinkEntry.lblSearchNo"),
										LangResources.getText("SWTRegisterDrinkEntry.lblSearchNo")
										});
					txtNoStart.setFocus();
					return;
				}
			}
			
			//Search action
			RegisterDrinkForm form = new RegisterDrinkForm();
			form.setActionForm("registerDrinkForm");
			form.setActionMethod("getDrinkList");
			
			form.setNoStart(Utils.toEmpty(txtNoStart.getText()));
			form.setNoEnd(Utils.toEmpty(txtNoEnd.getText()));
			
			form = (RegisterDrinkForm)execute(form);
			
			SWTUtils.OperatorUI.setEnableAll(grpSearch.getChildren(), false);
			Control[] infoGrp = new Control[]{
					tblInfo,
					btnDelete,
					btnNew,
					btnEdit
			};
			SWTUtils.OperatorUI.setEnableAll(infoGrp, true);
			SWTUtils.OperatorUI.setEnableAll(grpDetail.getChildren(), false);
			
			btnSave.setEnabled(false);
			
			drinkList = form.getList();
			displayDrinkList(form.getList());
			
			tblInfo.setFocus();
			
		} catch (SessionTimeOutException e) {
			log.error("SessionTimeOutException", e);
		} catch (ServerException e) {
			log.error("ServerException.", e);
			fatalErrorMsgBox();
		} catch (FatalException e) {
			log.error("FatalException.", e);
			fatalErrorMsgBox();
		}
	}

	/**
	 * display table program
	 * @param list
	 */
	private void displayDrinkList(ArrayList list) {

		tblInfo.removeAll();
		
		int rowcount = TBL_INFO_ROW_COUNT;
		if (list.size() >= rowcount){
			rowcount = list.size() + 2;
		}
		for (int i = 0; i < rowcount; i++) {
			TableTreeItem parent = new TableTreeItem(tableTree, SWT.NONE);
			parent.setBackground(BaseConstant.COLOR_TABLE_ROW_EVEN);
			
			if (i < list.size()){
				MstDrinkDTO drink = (MstDrinkDTO)list.get(i);
				
				int idx = 0;
				parent.setText(idx++, drink.getName());			//name
				parent.setText(idx++, drink.getTypeName());		//type
				parent.setText(idx++, "");						//price
				parent.setText(idx++, "");						//unit
				parent.setText(idx++, drink.getDescription());	//description
				
				parent.setData(Const.ITEMID, drink);
				parent.setData(ID, String.valueOf(i));
				
				ArrayList priceList = drink.getPriceList();
				for (int j = 0; j < priceList.size(); j++) {
					MstPriceDTO price = (MstPriceDTO)priceList.get(j);
					TableTreeItem child = new TableTreeItem(parent, SWT.NONE);
					
					idx = 0;
					child.setText(idx++, "");									//name
					child.setText(idx++, "");									//type
					child.setText(idx++, Utils.formatDouble(price.getPrice()));	//price
					String unit = "";
					if (!Utils.isNullEmpty(price.getMoneyTypeName())
							&& !Utils.isNullEmpty(drink.getUnitName())){
						unit += price.getMoneyTypeName() + "/" + drink.getUnitName();;
					}
					child.setText(idx++, unit);									//unit
					child.setText(idx++, "");									//description
					
					child.setData(Const.ITEMID, drink);
					child.setData(ID, String.valueOf(i));
				}
				
				//Expand the parent item
				parent.setExpanded(true);
				
				if (Const.MODE_NEW.equals(drink.getProcessMode())){
					parent.setBackground(BaseConstant.COLOR_TABLE_ROW_NEW);
				} else if (Const.MODE_EDIT.equals(drink.getProcessMode())){
					parent.setBackground(BaseConstant.COLOR_TABLE_ROW_UPDATE);
				} else if (Const.MODE_DEL.equals(drink.getProcessMode())){
					parent.setBackground(BaseConstant.COLOR_TABLE_ROW_DELETE);
				}
			}
		}
	}

	/**
	 * editAction
	 */
	protected void editAction() {
		
		try {
			clblMessage.setText("");
			TableTreeItem item = tableTree.getSelection()[0];
			MstDrinkDTO drink = (MstDrinkDTO)item.getData(Const.ITEMID);
			if (null == drink){
				MsgBoxUtils.errorMsgBox("errors.NOT_SELECTED_RECORD");
				btnEdit.setFocus();
				return;
			}
			
			this.mode = Const.MODE_EDIT;
			edittingRow = Integer.parseInt((String)item.getData(ID));
			
			//init control
			SWTUtils.OperatorUI.setEnableAll(grpDetail.getChildren(), true);
			btnSave.setEnabled(true);
			//clear text detail area
			SWTUtils.OperatorUI.clearAll(grpDetail.getChildren());
			
			//set data row to detail group
			clblDrinkNo.setText(drink.getDrinkNo());
			txtDrinkName.setText(drink.getName());
			SWTUtils.ComboProcess.setDisplayLabel(cmbType, drink.getType());
			SWTUtils.ComboProcess.setDisplayLabel(cmbUnit, drink.getUnitCd());
			txtDescription.setText(drink.getDescription());
			
			priceList = null;
			if (null != drink.getPriceList() && !drink.getPriceList().isEmpty()){
				priceList = (ArrayList)drink.getPriceList().clone();
			}
			displayPriceList(priceList);
			
			txtDrinkName.setFocus();
			
		} catch (ClientException e) {
			log.error("ClientException.", e);
			fatalErrorMsgBox();
		}
	}
	
	/**
	 * displayPriceList
	 * @param priceList
	 */
	private void displayPriceList(ArrayList list) {

		tblUnitPrice.removeAll();
		
		int rowcount = TBL_PRICE_ROW_COUNT;
		if (list != null && list.size() >= rowcount){
			rowcount = list.size() + 2;
		}
		for (int i = 0; i < rowcount; i++) {
			TableItem tblItem = new TableItem(tblUnitPrice, SWT.NONE);
			
			if (list != null && i < list.size()){
				MstPriceDTO price = (MstPriceDTO)list.get(i);
				
				String[] items = new String[tblUnitPrice.getColumnCount()];
				int idx = 0;
				
				items[idx++] = "";
				items[idx++] = Utils.formatDouble(price.getPrice());
				items[idx++] = price.getMoneyTypeName();
				
				tblItem.setText(items);
				tblItem.setData(price);
				tblItem.setData(ID, price.getPriceNo());
			}
			
			if (i%2 == 0){
				tblItem.setBackground(BaseConstant.COLOR_TABLE_ROW_EVEN);
			}
		}
		tblUnitPrice.setData(list);
	}

	/**
	 * newAction
	 */
	protected void newAction() {
		
		this.mode = Const.MODE_NEW;
		clblMessage.setText("");
		this.priceList = null;
		
		//clear text detail area
		SWTUtils.OperatorUI.clearAll(grpDetail.getChildren());
		SWTUtils.OperatorUI.setEnableAll(grpDetail.getChildren(), true);
		btnSave.setEnabled(true);
		txtDrinkName.setFocus();
	}

	/**
	 * saveAction
	 */
	protected void saveAction() {
		
		try {
			clblMessage.setText("");
			if (!saveFlg){
				MsgBoxUtils.warningMsgBox("warning.NO_DATA_SAVE");
				return;
			}
			
			//lock screen
			lockScreen(true);
			
			RegisterDrinkForm form = new RegisterDrinkForm();
			form.setList(drinkList);
			
			form.setActionForm("registerDrinkForm");
			form.setActionMethod("saveAction");
			form = (RegisterDrinkForm)execute(form);
			
			SWTUtils.OperatorUI.clearAll(grpDetail.getChildren());
			
			//unlock screen
			lockScreen(false);
			
			searchAction();
			
			MsgBoxUtils.setStatusLabelMessage(clblMessage, "info.SAVE_SUCCESSFUL");
		
		} catch (SessionTimeOutException e) {
			log.error("SessionTimeOutException.", e);
			
		} catch (FatalException e) {
			log.error("FatalException.", e);
			fatalErrorMsgBox();
		}
	}
	
	/**
	 * apply action
	 *
	 */
	@SuppressWarnings("unchecked")
	protected void applyAction() {

		clblMessage.setText("");
		if (!checkData()){
			return;
		}
		//
		MstDrinkDTO drink = null;
		if (mode.equals(Const.MODE_NEW)){
			drink = new MstDrinkDTO();
			drink.setProcessMode(Const.MODE_NEW);
		} else {
			drink = (MstDrinkDTO)drinkList.get(edittingRow);
			if (Utils.isNullEmpty(drink.getDrinkNo())){//in this case, reedit a new room
				drink.setProcessMode(Const.MODE_NEW);
			}else{
				drink.setProcessMode(Const.MODE_EDIT);
			}
		}
		//
		drink.setName(Utils.toEmpty(txtDrinkName.getText()));
		drink.setDescription(Utils.toEmpty(txtDescription.getText()));
		drink.setUnitCd(SWTUtils.ComboProcess.getSelectingValue(cmbUnit));
		drink.setUnitName(SWTUtils.ComboProcess.getSelectingLabel(cmbUnit));
		drink.setType(SWTUtils.ComboProcess.getSelectingValue(cmbType));
		drink.setTypeName(SWTUtils.ComboProcess.getSelectingLabel(cmbType));
		drink.setPriceList(priceList);
		priceList = null;
		//
		if (mode.equals(Const.MODE_NEW)){
			drinkList.add(drink);
		} else {
			drinkList.set(edittingRow, drink);
		}
		displayDrinkList(drinkList);
		//
		SWTUtils.OperatorUI.clearAll(grpDetail.getChildren());
		SWTUtils.OperatorUI.setEnableAll(grpDetail.getChildren(), false);
		saveFlg = true;
		tblInfo.setFocus();
	}

	/**
	 * check data input
	 * @return boolean
	 */
	private boolean checkData() {
		
		try {
			if (!SWTUtils.Check.checkRequired(txtDrinkName, 
					LangResources.getText("SWTRegisterDrinkEntry.txtDrinkName"))){
				return false;
			}
			
			if (!SWTUtils.Check.checkRequired(cmbUnit, 
					LangResources.getText("SWTRegisterDrinkEntry.lblUnit"))){
				cmbUnit.setFocus();
				return false;
			}
			
		} catch (ClientException e) {
			log.error("ClientException.", e);
			fatalErrorMsgBox();
			return false;
		}
		return true;
	}

	/**
	 * init data to display
	 * @throws FatalException
	 */
	@SuppressWarnings("unchecked")
	private boolean initData() throws FatalException {
	
		try {
			Map map = new HashMap();
			map.put(Const.SYSTEM_CONFIG.MONEY_TYPE, Boolean.FALSE);
			map.put(Const.SYSTEM_CONFIG.DRINK_TYPE, Boolean.FALSE);
//			map = SWTUtils.ComboProcess.getComboInitItems(map, this);
			
			String[] items = (String[])map.get(Const.SYSTEM_CONFIG.MONEY_TYPE);
			if (items == null || items.length == 0 || Utils.isNullEmpty(items[1])){
				MsgBoxUtils.errorMsgBox("errors.COMBO_DATA_NOT_SET", 
						new Object[]{LangResources.getText("SWTRegisterDrinkEntry.cmbMoney")});
				return false;
			}
			cmbMoney.setItems(items);
			
			items = null;
			items = (String[])map.get(Const.SYSTEM_CONFIG.DRINK_TYPE);
			if (items == null || items.length == 0 || Utils.isNullEmpty(items[1])){
				MsgBoxUtils.errorMsgBox("errors.COMBO_DATA_NOT_SET", 
						new Object[]{LangResources.getText("SWTRegisterDrinkEntry.lblType")});
				return false;
			}
			cmbType.setItems(items);
			
			RegisterFoodForm form = new RegisterFoodForm();
			form.setActionForm("registerFoodForm");
			form.setActionMethod("getInitData");
			
			form = (RegisterFoodForm)execute(form);
			
			items = null;
			ArrayList ls = form.getList();
			if (ls == null || ls.isEmpty()){
				MsgBoxUtils.errorMsgBox("errors.COMBO_DATA_NOT_SET", 
						new Object[]{LangResources.getText("SWTRegisterDrinkEntry.lblUnit")});
				return false;
			}
			items = new String[ls.size()];
			for (int i = 0; i < ls.size(); i++) {
				MstUnitDTO unit = (MstUnitDTO)ls.get(i);
				items[i] = unit.getUnitCd() + Const.COMBO_SEPERATE + unit.getName();
			}
			cmbUnit.setItems(items);
			
		} catch (SessionTimeOutException e) {
			log.warn("SessionTimeOutException.", e);
		}
		return true;
	}

	/**
	 * lockScreen
	 * @param flg 
	 */
	protected void lockScreen(boolean flg) {
		
		SWTUtils.OperatorUI.setEnableAll(this.getChildren(), !flg);
	}

	/**
	 * add/apply price
	 *
	 */
	@SuppressWarnings("unchecked")
	private void addPriceAction() {
		
		try {
			clblMessage.setText("");
			
			if (Utils.isNullEmpty(txtUnitPrice.getText())){
				MsgBoxUtils.errorMsgBox("errors.NOT_NULL", 
						new Object[]{LangResources.getText("SWTRegisterDrinkEntry.txtUnitPrice")});
				txtUnitPrice.setFocus();
				return;
			}
			if (!Utils.isNumeric(Utils.toNormalNumber(txtUnitPrice.getText()))){
				MsgBoxUtils.errorMsgBox("errors.NUMERIC_FORMAT", 
						new Object[]{LangResources.getText("SWTRegisterDrinkEntry.txtUnitPrice")});
				txtUnitPrice.setFocus();
				return;
			}
			//
			MstPriceDTO price = new MstPriceDTO();
			price.setPrice(Utils.toNormalNumber(txtUnitPrice.getText()));
			price.setMoneyType(SWTUtils.ComboProcess.getSelectingValue(cmbMoney));
			price.setMoneyTypeName(SWTUtils.ComboProcess.getSelectingLabel(cmbMoney));
			//
			if (null == priceList){
				priceList = new ArrayList();
			}
			// check exist of current rent type
			int existedIdx = chkExistedPrice(priceList, price.getMoneyType());
			if (existedIdx == -1){
				priceList.add(price);
			}else{
				priceList.set(existedIdx, price);
			}
			// Order price list
			SortUtil.sort(priceList, new String[]{"moneyType"}, SortUtil.ASC);
			
			displayPriceList(priceList);
			tblUnitPrice.setSelection(existedIdx);
			//
			txtUnitPrice.setText("");
			tblUnitPrice.setFocus();
			tblUnitPrice.showSelection();
			
		} catch (ClientException e) {
			log.error("ClientException.", e);
			fatalErrorMsgBox();
		}
	}

	/**
	 * chkExistedPrice
	 * @param priceList2
	 * @param moneyType
	 * @return int
	 */
	private int chkExistedPrice(ArrayList list, String moneyType) {
		int idx = -1;
		for (int i = 0; i < list.size(); i++) {
			MstPriceDTO price = (MstPriceDTO)list.get(i);
			if (price.getMoneyType().equals(moneyType)){
				return i;
			}
		}
		return idx;
	}

	/**
	 * remove price
	 *
	 */
	private void removePriceAction() {
		
		try {
			if (priceEditRow == -1){
				MsgBoxUtils.errorMsgBox("errors.NOT_SELECTED_RECORD", new Object[]{});
				btnRemove.setFocus();
				return;
			}
			TableItem row = tblUnitPrice.getItem(priceEditRow);
			MstPriceDTO price = (MstPriceDTO)row.getData();
			if (null == price){
				MsgBoxUtils.errorMsgBox("errors.NOT_SELECTED_RECORD", new Object[]{});
				btnRemove.setFocus();
				return;
			}
	
			tblUnitPrice.remove(priceEditRow);
			priceList.remove(priceEditRow);
			priceEditRow = -1;
			//
			txtUnitPrice.setText("");
			btnRemove.setFocus();
			
		} catch (ClientException e) {
			log.error("ClientException.", e);
			fatalErrorMsgBox();
		}
	}

	@Override
	protected boolean loadData() throws FatalException {
		// TODO Auto-generated method stub
		return false;
	}
	
}